//
//  AppIntroduceVC.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/7/6.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "AppIntroduceVC.h"

#import "MainViewController.h"
#import "MainRecommendVC.h"

@interface AppIntroduceVC ()<UIScrollViewDelegate>

@property (nonatomic, strong)UIPageControl * pageControl;
@property (nonatomic)NSInteger currentPage;


@end

@implementation AppIntroduceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createAppIntroduce];
}

- (void)createAppIntroduce {
    
    [self createScrollViewWithImageArray:@[MYLocalizedString(@"GUIDE_PNG1", nil),MYLocalizedString(@"GUIDE_PNG2", nil),MYLocalizedString(@"GUIDE_PNG3", nil)]];
    [self createPageControl:3];
}

- (void)createScrollViewWithImageArray:(NSArray *)imageArray {
    
    CGFloat Width = SCREEN_WIDTH;
    CGFloat Hight = SCREEN_HEIGHT;
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    scrollView.contentSize = CGSizeMake(Width * imageArray.count, Hight);
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    for (int num = 0; num < imageArray.count; num ++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(num * Width, 0, Width, Hight)];
        [scrollView addSubview:imageView];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        
        //本地图片
        UIImage *image = [UIImage imageNamed:imageArray[num]];
        imageView.image = image;
        
        if (num == imageArray.count - 1) {
            imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoMain)];
            [imageView addGestureRecognizer:tap];
        }
    }
}

/**
 *  创建PageControl
 */
- (void)createPageControl:(NSInteger)pages {
    
    CGFloat bottomH = 30;
    
    if (ISiPhoneX) {
        bottomH = 64;
    }
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 150)/2, SCREEN_HEIGHT - bottomH,150,20)];
    [self.view addSubview:_pageControl];
    _pageControl.numberOfPages = pages;
    //    _pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    //    _pageControl.pageIndicatorTintColor = [UIColor groupTableViewBackgroundColor];
}

- (void)gotoMain {
    
    NSNumber *lastID = [AppManager sharedManager].lastCityID;
    
    if (!lastID) {
        
        MainRecommendVC *recommendVC = [[MainRecommendVC alloc] init];
        recommendVC.delegate = self;
        
        @weakify(self);
        [self presentViewController:recommendVC animated:YES completion:^{
            @strongify(self);
            UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"defaultBG"]];
            bgImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
            
            [self.view addSubview:bgImageView];
        }];
        
        return;
    }
    
    UIViewController *leftVC = [IGUtils loadViewController:@"LeftVC" storyboard:@"Account"];
    
    MainViewController *mainVC = [MainViewController new];
    
    [AppManager sharedManager].manVC = mainVC;
    BaseNavigationController *navigationController = [[BaseNavigationController alloc] initWithRootViewController:mainVC];
    
    LGSideMenuController *sideMenuController = [LGSideMenuController sideMenuControllerWithRootViewController:navigationController leftViewController:leftVC rightViewController:nil];
    
    sideMenuController.leftViewWidth = SCREEN_WIDTH * 4 / 5.0;
    sideMenuController.leftViewPresentationStyle = LGSideMenuPresentationStyleSlideAbove;
    
    [UIApplication sharedApplication].keyWindow.rootViewController = sideMenuController;
    
}

- (void)selectCityID:(NSString *)cityID {
    
    [AppManager sharedManager].lastCityID = @(cityID.integerValue);
    [self gotoMain];
    
}


#pragma mark - 实现代理

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x > self.view.bounds.size.width * 2) {
        [self gotoMain];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    _currentPage = scrollView.contentOffset.x/scrollView.frame.size.width;
    self.pageControl.currentPage = _currentPage;
    
}

@end

